﻿/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 10/5/2009
 * Time: 11:27 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.IO;
using popLib;
using popLib.ExtensionClass;
using System.Reflection;

namespace GadgetPreview
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : SidebarContainer
	{
		
		TransparentForm bgForm;
		List<SidebarGadget> gadgets = new List<SidebarGadget>();
		string[] args;
		int nextHeight;
		string tempDir = FileLib.getFullPath( "Copies" );
		
		public MainForm(string[] args)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
				
			TransparentFormOption options = new TransparentFormOption();
			options.ShowTitle = options.IsHandleOpacity = false;
			bgForm = FormLib.setTransparentForm( this, 255, FormBackground.VistaBlue, options );
			
			bgForm.DoubleClick += delegate { ReloadSidebar(); };
			
			FormLib.dragAndDrop( bgForm, delegate(Control control, string[] items){
			                    	this.args = items;
			                    	ReloadSidebar();
			                    } );
			
			//this.args = args;
			this.args = new string[]{"MailCheckerGadget.dll"};
			ReloadSidebar();
		}
		
		public override void ShowBalloonTip( string title, string message, ToolTipIcon toolTipIcon){
			nfi_tray.ShowBalloonTip( 5000, title, message, toolTipIcon );
		}
		
		public override void ReloadSidebar(){
			prepareBackground();			
			foreach( string gadget in args ) 
				loadGadget( gadget );
			reDrawForm();
		}
		
		void loadGadget( string filename ){
			
			filename = filename.Trim();
			if( !Path.IsPathRooted(filename) ) filename = FileLib.getFullPath( filename );
			if( !File.Exists( filename ) ) return;
			
			Assembly assembly = Assembly.LoadFile( filename );
			foreach (Type type in assembly.GetExportedTypes())
			{				
			    if ( type.GetInterface("ISidebarGadget", true) != null)
			    {
			        SidebarGadget obj = (SidebarGadget)type.InvokeMember(null,        
					BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic |
					BindingFlags.Instance | BindingFlags.CreateInstance, null, null , new object[]{ this });
			    	
					gadgets.Add( obj );
					UserControl child 	= (UserControl)obj.InitGadget();
					child.Location 		= new Point( this.Width/2 - child.Width/2, nextHeight );
					nextHeight 			+= child.Height + 5;
					this.Controls.Add( child );
					drawControlBackground( child );
			    }
			}
		}
		
		Bitmap bgTemp;
		Graphics painter;
		void prepareBackground(){			
			Controls.Clear();
			gadgets.Clear();
			
			nextHeight 		= 20;
			bgForm.Visible 	= this.Visible = false;
			bgTemp 			= new Bitmap(bgForm.Width, bgForm.Height);
			painter 		= Graphics.FromImage(bgTemp);
			painter.DrawImage( bgForm.bmpBackground, new Point(0,0) );
		}
		void drawControlBackground( Control control ){
			if( control.BackgroundImage == null ) return;
			Bitmap bitmap = (Bitmap)control.BackgroundImage;
			control.BackgroundImage = null;		
			painter.DrawImage( bitmap, control.Left, control.Top, control.Width, control.Height );
		}
		void reDrawForm(){			
			bgForm.reDrawBackground( bgTemp );			
			bgForm.Visible = this.Visible = true;
			bgTemp.Dispose();
			GC.Collect();
		}
		
	}
}
